package com.jxx.algorithm.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author Jxx
 * @Description: algorithm2:给定一个整数的计算公式计算其结果
 * @create 2021-11-02 22:46
 */
public class Calculator {
    public static void main(String[] args) {
        String expression = "100+4*(30-100/5)";
        List<String> mid = getMid(expression);
        System.out.println(mid);
        List<String> last = getLast(mid);
        System.out.println(last);
        System.out.println(calculate(last));
    }

    /**
     * 100+4*(30-100/5) 获取中缀表达式的List
     * @param expression
     * @return
     */
    public static List<String> getMid(String expression) {
        List<String> result = new ArrayList<String>();
        String s = expression;
        int i = 0;
        do {
            if (s.charAt(i) < 48 || s.charAt(i) > 57) {
                result.add("" + s.charAt(i));
                i++;
            } else {
                String str = "";
                while (i < s.length() && s.charAt(i) >= 48 && s.charAt(i) <= 57) {
                    str = str + s.charAt(i);
                    i++;
                }
                result.add(str);
            }
        } while (i < s.length());
        return result;
    }

    /**
     * 中缀--->后缀
     * @param list
     * @return
     */
    public static List<String> getLast(List<String> list) {
        Stack<String> stack = new Stack<>();
        List<String> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).matches("\\d+")) {
                result.add(list.get(i));
            } else if (list.get(i).equals("(")) {
                stack.push(list.get(i));
            } else if (list.get(i).equals(")")) {
                while(!stack.peek().equals("(")) {
                    result.add(stack.pop());
                }
                stack.pop();
            } else {
                while (stack.size() != 0 && Operator.getValue(list.get(i)) <= Operator.getValue(stack.peek())) {
                    result.add(stack.pop());
                }
                stack.push(list.get(i));
            }
        }
        while (stack.size() != 0) {
            result.add(stack.pop());
        }
        return result;
    }


    public static int calculate(List<String> ls) {
    Stack<String> stack = new Stack<String>();
    for (String s : ls) {
        if (s.matches("\\d+")) {
            stack.push(s);
        } else {
            int num2 = Integer.parseInt(stack.pop());
            int num1 = Integer.parseInt(stack.pop());
            int result = 0;
            if (s.equals("+")) {
                result = num1 + num2;
            } else if (s.equals("-")) {
                result = num1 - num2;
            } else if (s.equals("*")) {
                result = num1 * num2;
            } else if (s.equals("/")) {
                result = num1 / num2;
            } else {
                throw new RuntimeException("运算错误");
            }
            stack.push(""+result);
        }
    }
    return Integer.parseInt(stack.pop());
}
    public static int calculate(String expression) {
        List<String> mid = new ArrayList<>();
        int i = 0;
        do {
            if (expression.charAt(i) < 48 || expression.charAt(i) > 57) {
                mid.add(""+expression.charAt(i));
                i++;
            } else {
                String str = "";
                while (i < expression.length() && expression.charAt(i) >= 48 && expression.charAt(i) <= 57) {
                    str += expression.charAt(i);
                    i++;
                }
                mid.add(str);
            }
        } while(i < expression.length());
        System.out.println(mid);
        Stack<String> stack1 = new Stack<>();
        List<String> last = new ArrayList<>();
        for (String s : mid) {
            if (s.matches("\\d+")) {
                last.add(s);
            } else if (s.equals("(")) {
                stack1.push(s);
            } else if (s.equals(")")) {
                while (!stack1.peek().equals("(")) {
                    last.add(stack1.pop());
                }
                stack1.pop();
            } else {
                while (stack1.size() != 0 && Operator.getValue(stack1.peek()) >= Operator.getValue(s)) {
                    last.add(stack1.pop());
                }
                stack1.push(s);
            }
        }
        while (stack1.size() != 0) {
            last.add(stack1.pop());
        }
        System.out.println(last);
        Stack<String> stack2 = new Stack<>();
        for (String s : last) {
            if (s.matches("\\d+")) {
                stack2.push(s);
            } else {
                int num1 = Integer.parseInt(stack2.pop());
                int num2 = Integer.parseInt(stack2.pop());
                int tempResult = 0;
                if (s.equals("+")) {
                    tempResult = num2 + num1;
                } else if (s.equals("-")) {
                    tempResult = num2 - num1;
                } else if (s.equals("*")) {
                    tempResult = num2 * num1;
                } else if (s.equals("/")) {
                    tempResult = num2 / num1;
                } else {
                    throw new RuntimeException("运算错误");
                }
                stack2.push(""+tempResult);
            }
        }
        return Integer.parseInt(stack2.pop());
    }
}

class Operator {
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 2;
    private static int DIV = 2;

    public static int getValue(String s) {
        int result = 0;
        switch (s) {
            case "+" :
                result = ADD;
                break;
            case "-" :
                result = SUB;
                break;
            case "*" :
                result = MUL;
                break;
            case "/":
                result = DIV;
                break;
            default:
                System.out.println("操作符错误");
                break;
        }
        return result;
    }

}
